<?php

#=============================
# Set environment
#=============================
ini_set('register_globals', 0);

if ( @ini_get('register_globals') )
{
	while ( list($key, $value) = each($_REQUEST) )
	{
		unset( $$key );
	}
}

@set_time_limit(0);

session_name('SID');
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);

class captcha {

	#====================================
	# Generate CAPTCHA image
	#====================================
	function genimg()
	{
		$im = imagecreate(200, 50);
		$bgcolor = imagecolorallocate($im, 255, 255, 255);
		$noise = imagecolorallocate($im, 100, 100, 100);
		$fontmin = (150 * 0.15);
		$fontmax = (150 * 0.2);
		$lastchar = 35;
		
		for ($l = 0; $l < 50; $l++)
		{
			imagefilledellipse($im, rand(0, 200), rand(0, 50), 1, 1, $noise);
		}
		
		for ($l = 0; $l < 20; $l++)
		{
			imageline($im, rand(0, 200), rand(0, 50), rand(0, 200), rand(0, 50), $noise);
		}
		
		$_SESSION['captcha'] = "";
		
		for ($i = 0; $i < 6; $i++)
		{
			$_SESSION['captcha'] .= $this->gencode();
			$text = imagecolorallocate($im, rand(0, 150), rand(0, 150), rand(0, 150));
			$code = $_SESSION['captcha']{ $i };
			imagettftext($im, rand( $fontmin, $fontmax ), rand(-10, 10), $lastchar, 40, $text, 'arial.ttf', $code);
			$lastchar = ($lastchar + 25);
		}
		
		header('Content-Type: image/png');
		imagepng($im);
		imagedestroy($im);
	}
	
	#====================================
	# Produce a 6 character unique code
	#====================================
	function gencode()
	{
		$chars = '23456789bcdfghjkmnpqrstvwxyz';
		$code .= $chars{ rand(0, (strlen($chars) - 1)) };
		
		return $code;
	}
	
}

$img = new captcha();
$img->genimg();

?>